package com.hdd.biz.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.biz.app.entity.RewardRecordModel;
import com.hdd.biz.finance.dto.PaymentRequest;
import com.hdd.biz.finance.dto.PaymetListRequest;
import com.hdd.biz.finance.entity.Paymet;
import com.hdd.biz.finance.mapper.PaymetMapper;
import com.hdd.biz.finance.service.PaymetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.biz.member.entity.Member;
import com.hdd.biz.member.service.MemberService;
import com.hdd.dto.SuperResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付记录 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-03-09
 */
@Service
public class PaymetServiceImpl extends ServiceImpl<PaymetMapper, Paymet> implements PaymetService {


    @Autowired
    private MemberService memberService;

    @Override
    public SuperResponse list(PaymetListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Paymet> wrapper = new QueryWrapper();
        Page<Paymet> pages = new Page<>(request.getPageNo(),request.getPageSize());
        if(!io.micrometer.core.instrument.util.StringUtils.isEmpty(request.getStartTime())&&!io.micrometer.core.instrument.util.StringUtils.isEmpty(request.getEndTime())){
            wrapper.between("create_time",request.getStartTime(),request.getEndTime());
        }
        if(!StringUtils.isEmpty(request.getStatus())){
            wrapper.eq("status",request.getStatus());
        }
        if(!StringUtils.isEmpty(request.getPaymentNo())){
            wrapper.eq("payment_no",request.getPaymentNo());
        }
        wrapper.orderByDesc("create_time");
        IPage<Paymet> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Paymet> list = mapIPage.getRecords();
        Member member = null;
        for(Paymet p:list){
            member = memberService.findMemberByUid(p.getWorksUserId());
            p.setAuthorName(member.getNickname());
            member = memberService.findMemberByUid(p.getUserId());
            p.setRewardName(member.getNickname());
        }
        response.setItems(list);
        response.setTotals(mapIPage.getTotal());
        return response;
    }

    @Override
    public SuperResponse checkOrder(PaymentRequest request) {
        SuperResponse response = new SuperResponse();
        return response;
    }


    @Override
    public boolean insertPaymeny(Paymet paymet) {
        baseMapper.insert(paymet);
        return false;
    }


    @Override
    public List<RewardRecordModel> rewardRecord(String worksId, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("worksId",worksId);
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        return baseMapper.rewardRecord(map);
    }

    @Override
    public int rewardCount(String worksId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status","T");
        wrapper.eq("works_no",worksId);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public Paymet findPayMent(String paymentNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("payment_no",paymentNo);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public double totalPaymetAmount() {
        return baseMapper.totalPaymetAmount();
    }

    @Override
    public double todayPaymetAmount() {
        return baseMapper.todayPaymetAmount();
    }

    @Override
    public double yesterdayPaymetAmount() {
        return baseMapper.yesterdayPaymetAmount();
    }

    @Override
    public double weekPaymetAmount() {
        return baseMapper.weekPaymetAmount();
    }

    @Override
    public double monthPaymetAmount() {
        return baseMapper.monthPaymetAmount();
    }

    @Override
    public Integer totalPaymet() {
        return baseMapper.totalPaymet();
    }

    @Override
    public Integer todayPaymet() {
        return baseMapper.todayPaymet();
    }

    @Override
    public Integer yesterdayPaymet() {
        return baseMapper.yesterdayPaymet();
    }

    @Override
    public Integer weekPaymet() {
        return baseMapper.weekPaymet();
    }

    @Override
    public Integer monthPaymet() {
        return baseMapper.monthPaymet();
    }
}
